﻿using System;
using System.Linq;
using System.Web;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Code
{
	public class MemberBasePage : BasePage
	{
		/// <summary>
		/// 当前登录的用户信息
		/// </summary>
		public static TB_User UserInfo
		{
			get
			{
				TB_User user = HttpContext.Current.Session["UserInfo"] as TB_User;
				if (user != null)
				{
					return user;
				}
				return null;
			}
		}

		/// <summary>
		/// 会员是否登录
		/// </summary>
		public static bool IsLogin
		{
			get
			{
				return UserInfo is TB_User && UserInfo != null;
			}
		}

		/// <summary>
		/// 验证当前登录的用户是否拥有当前页面的读权限
		/// </summary>
		protected void ValidRight()
		{
			var pageInfo = SystemMapCache.Instance[SystemCode];
			if(pageInfo != null && pageInfo.Status)
			{
				//受控页面，需要登录
				if(pageInfo.NeedMemberLogin)
				{
					if (IsLogin)
					{
						if (!CheckCodeRight(UserInfo, SystemCode))// || !CheckReadRight(UserInfo, Request.Path))//无权访问，跳转到首页
						{
							RedirectWithMessage(MemberDefaultPage, "您无权访问当前页面!");
						}
					}
					else//未登录，跳转登录页面
					{
						RedirectWithMessage(MemberLoginPage, "当前页面需要登录后才可访问,请登录!");
					}
				}
			}
			else
			{
				RedirectWithMessage(MemberDefaultPage, "您访问的页面不存在!");
			}
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			ValidRight();
		}

		/// <summary>
		/// 判断当前登录管理员是否拥有指页面的读权限
		/// </summary>
		/// <param name="user"></param>
		/// <param name="requestPath"></param>
		/// <returns></returns>
		public static bool CheckReadRight(TB_User user, string requestPath)
		{
			if (user != null && UserRightCache.Instance[user.UserName] != null && UserRightCache.Instance[user.UserName].Count > 0)
			{
				//判断当前用户是否拥有指定URL的访问权限
				bool valided = (UserRightCache.Instance[user.UserName].Where(p => (SystemMapCache.Instance.List.Where(s => s.EURL.ToLower().Contains(requestPath.ToLower().Trim()) || s.CURL.ToLower().Contains(requestPath.ToLower().Trim())).Select(s => s.Code).Contains(p.Code))).Count() > 0);
				return valided;
			}
			return false;
		}

		/// <summary>
		/// 检查用户是否有指定页面的写权限
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public static bool CheckWriteRight(TB_User user, string requestPath)
		{
			if (user != null && UserRightCache.Instance[user.UserName] != null && UserRightCache.Instance[user.UserName].Count > 0)
			{
				//判断当前用户是否拥有指定URL的写权限
				bool valided = (UserRightCache.Instance[user.UserName].Where(p => p.Type && (SystemMapCache.Instance.List.Where(s => s.EURL.ToLower().Contains(requestPath.ToLower().Trim()) || s.CURL.ToLower().Contains(requestPath.ToLower().Trim())).Select(s => s.Code).Contains(p.Code))).Count() > 0);
				return valided;
			}
			return false;
		}

		/// <summary>
		/// 判断指定用户是否拥有访问指定业务代码的权限
		/// </summary>
		/// <param name="user"></param>
		/// <param name="code"></param>
		/// <returns></returns>
		public static bool CheckCodeRight(TB_User user, string code)
		{
			if (user != null && UserRightCache.Instance[user.UserName] != null && UserRightCache.Instance[user.UserName].Count > 0)
			{
				//判断当前用户是否拥有指定Code的权限
				bool valided = (UserRightCache.Instance[user.UserName].Where(p => p.Code == code).Count() > 0);
				return valided;
			}
			return false;
		}
	}
}